<?php

include '../../init.php';

// 是否登录
$uid = @$_SESSION['uid'];
if(@!$_SESSION['is_login'] || empty($uid)) {
    show_msg(1,'请先登录');
    exit();
}


// 申请版主列表
$zid = @$_GET['zid'];


// 拦截是否申请版主
$send = @$_GET['send'];
if ($send == 1) {
    // 申请版主
    $time = time();
    $data = array();
    $sql = "INSERT INTO `relo_tb`(modulid,uid,create_time,update_time) VALUES({$zid},{$uid},{$time},{$time}) ;";
    if (!my_mysql_query($sql, $data)) {
        show_msg(1, '申请失败,稍后重试');
        exit();
    }
    show_msg(0, '申请成功,等待后台的审核');
    exit();

} else if ($send == 2) {
    // 下次再来
    header('Location:./index.php?zid='.$zid);
    exit();
}

// 检测登录用户是否是版主
$data = array(); 
$sql  = "SELECT rid FROM `relo_tb` WHERE modulid={$zid} AND uid={$uid} ";
my_mysql_query($sql,$data);
if (count(@$data[0]['rid']) > 0){
    show_msg(0, '你已经是管理员了', 2, './index.php?zid='.$zid);
    exit();
}


// 是否积分够了
$score = array();
$sql = "SELECT score FROM `login_tb` WHERE uid={$uid} LIMIT 1;";
if(!my_mysql_query($sql, $score)) {
    show_msg(1,'获取积分错误,请稍后重试');
    exit();
}
$score = $score[0]['score'];

if($score < 1000) {
    $tip = "你的积分有少于 1000 积分还不能申请版主";
    $button = "下次再来";
} else {
    $tip = "你的积分有:<span style=\"color:green\"> {$score} </span> 能够申请版主";
    $button = "现在申请";
}
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>版主申请</title>
        <meta charset="utf-8">
        <style type="text/css">
        *{
            padding: 0;
            margin: 0;
        }
        .main-div {
            width: 960px;
            height: 500px;
            padding: 5px;
            margin: 25px auto;
            font-size: 24px;
            text-align: center;
            line-height: 250px;
            background-color: #F8F8F8;
            border: 1px solid rgb(240,240,240);
        }
        button {
            line-height: 24px;
            border-radius: 5px;
            height: 38px;
            width: 120px;
            font-size: 20px;
            cursor: pointer;
            border:1px solid rgb(240,240,240);
        }
        </style>
    </head>

    <body>
        <div class="main-div">
            <p><?php echo $tip; ?></p>
            <form action="moderator.php" method="GET">
                <button type="submit" name="zid" value="<?php echo $zid; ?>">
                <input type="hidden" name="send" value=<?php echo $score<1000?"2":"1"; ?>>
                <?php
                    echo $button;
                ?>
                </button>
            </form>
        </div>
    </body>
</html>

